<?php

if(isset($_GET['do']))
{
	$mcrp = './../';
	include_once($mcrp . 'functions/session.php');
	if($mcuser->isadmin == 0) exit;
	
	if($_GET['do'] == 'del')
	{
		DelMessage($_GET['id']);
	}
	else if($_GET['do'] == 'new')
	{
		NewMessage($_GET['txt']);
	}
}
	
function GetAllMessages()
{
	global $mcrp;
	$msg = file_get_contents($mcrp . 'admin/messages.txt');
	if($msg == '') return array();
	$msg = explode("¤", $msg);
	foreach($msg as $m)
	{
		$msgf[] = array('txt' => $m, 'md5' => md5($m));
	}
	return $msgf;
}

function SaveMessages($msg)
{
	global $mcrp;
	foreach($msg as $m)
	{
		$msgf[] = $m['txt'];
	}
	$msgf = implode("¤", $msgf);
	file_put_contents($mcrp . 'admin/messages.txt', $msgf);
}

function NewMessage($t)
{
	$msg = GetAllMessages();
	array_unshift($msg, array('txt' => $t, 'md5' => 0));
	SaveMessages($msg);
	echo md5($t);
}

function DelMessage($n)
{
	$msg = GetAllMessages();
	foreach($msg as $i => $m)
	{
		if($m['md5'] == $n)
		{
			unset($msg[$i]);
			break;
		}
	}
	SaveMessages($msg);
}

?>